/**
 * Pownce API JavaScript library
 * (c) 2008 Jonathan Rohan http://twitter.com/jonrohan
 * v1.0.2
 */
var POWNCE_API_URL = "http://api.pownce.com/";
var POWNCE_API_VERSION = "2.0";

document.write("<iframe id=\"powncejavascriptapiframe\" style=\"display:none;\"></iframe>");

var pownce = {
    dom: {
        iframedoc: function(){
            var iframedoc;
            if (document.getElementById("powncejavascriptapiframe").contentWindow) {
                iframedoc = document.getElementById("powncejavascriptapiframe").contentWindow.document;
            }
            else {
                iframedoc = document.getElementById("powncejavascriptapiframe").contentDoc;
            }
            return iframedoc;
        },
        publicnotelist: null,
        publicnote: null,
        recipientlist: null,
        userprofile: null,
        userfollowers: null,
        userpublicnotes: null,
		functions : {
	        publicnotelist: function() { },
	        publicnote: function() { },
	        recipientlist: function() { },
	        userprofile: function() { },
	        userfollowers: function() { },
	        userpublicnotes: function() { }
		}
    },
    api: {
        urls: {
            publicnotelist: "/note_lists.json?callback=publicnotelistcallback{since_id}{type}{limit}{page}",
            publicnote: "/notes/{note_id}.json?callback=publicnotecallback{show_replies}{recipient_limit}",
            recipientlist: "/notes/{note_id}/recipients.json?callback=recipientlistcallback{limit}{page}",
            userprofile: "/users/{username}.json?callback=userprofilecallback",
            userfollowers: "/users/{username}/{relationship}.json?callback=userfollowerscallback{limit}{page}",
            userpublicnotes: "/note_lists/{username}.json?callback=userpublicnotelistcallback{since_id}{type}{limit}{page}"
        },
        json: {
            publicnotelist: null,
            publicnote: null,
            recipientlist: null,
            userprofile: null,
            userfollowers: null,
            userpublicnotes: null
        },
        notes: {
            publiclist: function(options){
                var url = pownce.util.prepareUrl(pownce.api.urls.publicnotelist, options);
                var iframedoc = pownce.dom.iframedoc();
                
                var script = document.createElement("script");
                script.type = "text/javascript";
                script.language = "Javascript";
                script.src = url;
                pownce.dom.publicnotelist = script;
                pownce.dom.functions.publicnotelist = options.onCallBack;
				try {
                    iframedoc.getElementsByTagName("head")[0].appendChild(script);
                } 
                catch (e) {
					document.getElementsByTagName("head")[0].appendChild(script);
				}
            },
            publicnote: function(options){
                if (!options.note_id) 
                    return;
                var url = pownce.util.prepareUrl(pownce.api.urls.publicnote, options);
                var iframedoc = pownce.dom.iframedoc();
                
                var script = document.createElement("script");
                script.type = "text/javascript";
                script.language = "Javascript";
                script.src = url;
                pownce.dom.publicnote = script;
                pownce.dom.functions.publicnote = options.onCallBack;
				try {
                    iframedoc.getElementsByTagName("head")[0].appendChild(script);
                } 
                catch (e) {
					document.getElementsByTagName("head")[0].appendChild(script);
				}
            },
            recipientlist: function(options){
                if (!options.note_id) 
                    return;
                var url = pownce.util.prepareUrl(pownce.api.urls.recipientlist, options);
                var iframedoc = pownce.dom.iframedoc();
                
                var script = document.createElement("script");
                script.type = "text/javascript";
                script.language = "Javascript";
                script.src = url;
                pownce.dom.recipientlist = script;
                pownce.dom.functions.recipientlist = options.onCallBack;
				try {
                    iframedoc.getElementsByTagName("head")[0].appendChild(script);
                } 
                catch (e) {
					document.getElementsByTagName("head")[0].appendChild(script);
				}
            }
        },
        user: {
            profile: function(options){
                if (!options.username) 
                    return;
                var url = pownce.util.prepareUrl(pownce.api.urls.userprofile, options);
                var iframedoc = pownce.dom.iframedoc();
                var script = document.createElement("script");
                script.type = "text/javascript";
                script.src = url;
                pownce.dom.userprofile = script;
                pownce.dom.functions.userprofile = options.onCallBack;
                try {
                    iframedoc.getElementsByTagName("head")[0].appendChild(script);
                } 
                catch (e) {
					document.getElementsByTagName("head")[0].appendChild(script);
                }
            },
            followers: function(options){
                if (!options.username) 
                    return;
                if (!options.relationship) 
                    return;
                var url = pownce.util.prepareUrl(pownce.api.urls.userfollowers, options);
                var iframedoc = pownce.dom.iframedoc();
                var script = document.createElement("script");
                script.type = "text/javascript";
                script.src = url;
                pownce.dom.userpublicnotes = script;
                pownce.dom.functions.userfollowers = options.onCallBack;
                try {
                    iframedoc.getElementsByTagName("head")[0].appendChild(script);
                } 
                catch (e) {
					document.getElementsByTagName("head")[0].appendChild(script);
                }
            },
            publicnotes: function(options){
                if (!options.username) 
                    return;
                var url = pownce.util.prepareUrl(pownce.api.urls.userpublicnotes, options);
                var iframedoc = pownce.dom.iframedoc();
                var script = document.createElement("script");
                script.type = "text/javascript";
                script.src = url;
                pownce.dom.userpublicnotes = script;
                pownce.dom.functions.userpublicnotes = options.onCallBack;
                try {
                    iframedoc.getElementsByTagName("head")[0].appendChild(script);
                } 
                catch (e) {
					document.getElementsByTagName("head")[0].appendChild(script);
                }
            }
        }
    },
    components: {
        publicNotesWidget: function(){
        
            document.write("<div class=\"pownce-public-notes-widget\" id=\"pownce-public-notes-widget\"></div>");
           
            var callBack = function(){

				var widgetbox = document.getElementById("pownce-public-notes-widget");
				var ul = document.createElement("ul");
                for (i = 0; i < pownce.api.json.publicnotelist.notes.length; i++) {
					var note = pownce.api.json.publicnotelist.notes[i];
					var li = document.createElement("li");
					
					var profimglnk = document.createElement("a");
					profimglnk.href = note.sender.permalink;
					var profimg = document.createElement("img");
					profimg.src = note.sender.profile_photo_urls.medium_photo_url;
					profimg.className = "user";
					profimg.width = "48";
					profimg.height = "48";
					profimg.title = note.sender.short_name;
					profimglnk.appendChild(profimg);
					if(note.sender.is_pro) {
						var proimgem = document.createElement("em");
						proimgem.className = "pro-2";
						proimgem.innerHTML = "Pro!";
						profimglnk.appendChild(proimgem);
					}
					li.appendChild(profimglnk);
					
					var pbdy = document.createElement("p");
					pbdy.className = "body";
					pbdy.innerHTML = note.body;
					var spn = document.createElement("span");
					spn.innerHTML = "...";
					pbdy.appendChild(spn);
					li.appendChild(pbdy);
					
					var detl = document.createElement("h4");
					
					var tmago = document.createElement("abbr");
					tmago.title = note.display_since;
					tmago.className = "published";
					tmago.innerHTML = note.display_since;
					detl.appendChild(tmago);
					
					var postr = document.createElement("span");
					postr.className = "vcard author";
					var postrlnk = document.createElement("a");
					postrlnk.className = "sender url";
					postrlnk.href = note.sender.permalink;
					var postrnm = document.createElement("span");
					postrnm.className = "fn";
					postrnm.innerHTML = note.sender.short_name;
					postrlnk.appendChild(postrnm);
					if(note.sender.is_pro) {
						var protxt = document.createTextNode(" - Pro!");
						postrlnk.appendChild(protxt);
					}
					postr.appendChild(postrlnk);
					detl.appendChild(postr);
					
					var repls = document.createElement("a");
					repls.className = "reply";
					repls.href = note.permalink;
					var rep = "";
					if(note.num_replies!="0") {
						if (note.num_replies == "1") {
							rep = document.createTextNode(note.num_replies + " Reply");
						}
						else {
							rep = document.createTextNode(note.num_replies + " Replies");
						}
					}
					else {
						rep = document.createTextNode("Reply!");
					}
					repls.appendChild(rep);
					detl.appendChild(repls);
					
					li.appendChild(detl);
					
					ul.appendChild(li);
                }
                var header = document.createElement("div");
				header.className = "header";
				widgetbox.appendChild(header);
				widgetbox.appendChild(ul);
            };
            pownce.api.notes.publiclist({limit:5,onCallBack:callBack});
           
        },
        userPublicNotesWidget: function(options){
        
            document.write("<div class=\"pownce-user-public-notes-widget\" id=\"pownce-user-public-notes-widget\"></div>");
           
            var callBack = function(){

				var widgetbox = document.getElementById("pownce-user-public-notes-widget");
				var ul = document.createElement("ul");
                for (i = 0; i < pownce.api.json.userpublicnotes.notes.length; i++) {
					var note = pownce.api.json.userpublicnotes.notes[i];
					var li = document.createElement("li");
					if (i == 0) {
						li.className = "lastPost";
		                var sname = document.createElement("h1");
		                sname.className = "fn nickname";
		                sname.innerHTML = note.sender.short_name;
		                li.appendChild(sname);

						var profimglnk = document.createElement("a");
						profimglnk.href = note.sender.permalink;
						var profimg = document.createElement("img");
						profimg.src = note.sender.profile_photo_urls.large_photo_url;
						profimg.className = "user";
						profimg.width = "100";
						profimg.height = "100";
						profimg.title = note.sender.short_name;
						profimglnk.appendChild(profimg);
						if (note.sender.is_pro) {
							var proimgem = document.createElement("em");
							proimgem.className = "pro-2";
							proimgem.innerHTML = "Pro!";
							profimglnk.appendChild(proimgem);
						}
						li.appendChild(profimglnk);
					}
					var pbdy = document.createElement("p");
					pbdy.className = "body";
					pbdy.innerHTML = note.body;
					var spn = document.createElement("span");
					spn.innerHTML = "...";
					pbdy.appendChild(spn);
					li.appendChild(pbdy);
					
					var detl = document.createElement("h4");
					
					var tmago = document.createElement("abbr");
					tmago.title = note.display_since;
					tmago.className = "published";
					tmago.innerHTML = note.display_since;
					detl.appendChild(tmago);

					var repls = document.createElement("a");
					repls.className = "reply";
					repls.href = note.permalink;
					var rep = "";
					if(note.num_replies!="0") {
						if (note.num_replies == "1") {
							rep = document.createTextNode(note.num_replies + " Reply");
						}
						else {
							rep = document.createTextNode(note.num_replies + " Replies");
						}
					}
					else {
						rep = document.createTextNode("Reply!");
					}
					repls.appendChild(rep);
					detl.appendChild(repls);
					
					li.appendChild(detl);
					
					ul.appendChild(li);
                }
                var header = document.createElement("div");
				header.className = "header";
				widgetbox.appendChild(header);


				widgetbox.appendChild(ul);
            };
            options.onCallBack = callBack;
            pownce.api.user.publicnotes(options);

        },
        profileWidget: function(options){
           	document.write("<div class=\"pownce-profile-widget\" id=\"pownce-profile-widget\"></div>");
            var returnFunction = function(){
				
            
				var user = pownce.api.json.userprofile;
                
                var vcard = document.createElement("div");
                vcard.className = "intro vcard";
                
                var profurl = document.createElement("a");
                profurl.href = user.permalink;
                
                var profimg = document.createElement("img");
                profimg.src = user.profile_photo_urls.large_photo_url;
                profimg.width = "140";
                profimg.height = "140";
                profimg.title = user.short_name;
                profimg.alt = user.short_name;
                profimg.className = "user photo";
                
                profurl.appendChild(profimg);
                
                if (user.is_pro) {
                    var empro = document.createElement("em");
                    empro.className = "pro";
                    empro.innerHTML = "Pro!";
                    
                    profurl.appendChild(empro);
                }
                
                vcard.appendChild(profurl);
                
                var sname = document.createElement("h1");
                sname.className = "fn nickname";
                sname.innerHTML = user.short_name;
                
                vcard.appendChild(sname);
                
                var pdesc = document.createElement("p");
                pdesc.className = "description adr";
                
                var agesex = document.createTextNode(((user.age)?user.age + "-year-old ":"") + ((user.gender)?user.gender.toLowerCase() + " ":"") + (user.location?"from ":""));
                
                pdesc.appendChild(agesex);
                
                var loc = document.createElement("span");
                loc.className = "locality";
                loc.innerHTML = user.location;
                
                pdesc.appendChild(loc);
                
                vcard.appendChild(pdesc);

				var pdesc2 = document.createElement("p");
                pdesc2.className = "description note";
				
				pdesc2.innerHTML = user.blurb;
                vcard.appendChild(pdesc2);
				
                var header = document.createElement("div");
				header.className = "header";
				
                document.getElementById("pownce-profile-widget").appendChild(header);
                document.getElementById("pownce-profile-widget").appendChild(vcard);
                
            };
			
			options.onCallBack = returnFunction;
			
            pownce.api.user.profile(options);
            
            
        }
    },
    util: {
        prepareUrl: function(url, options){
            if (!options) 
                options = {};
            
            url = url.replace(/{type}/, ((options.type) ? "&type=" + options.type : ""));
            url = url.replace(/{since_id}/, ((options.since_id) ? "&since_id=" + options.since_id : ""));
            url = url.replace(/{limit}/, ((options.limit) ? "&limit=" + options.limit : ""));
            url = url.replace(/{page}/, ((options.page) ? "&page=" + options.page : ""));
            url = url.replace(/{note_id}/, ((options.note_id) ? options.note_id : ""));
            url = url.replace(/{show_replies}/, ((options.show_replies) ? "&show_replies=" + options.show_replies : ""));
            url = url.replace(/{recipient_limit}/, ((options.recipient_limit) ? "&recipient_limit=" + options.recipient_limit : ""));
            url = url.replace(/{username}/, ((options.username) ? options.username : ""));
            url = url.replace(/{rel}/, ((options.rel) ? options.rel : ""));
            url = url.replace(/{relationship}/, ((options.relationship) ? "&relationship=" + options.relationship : ""));
            
            return POWNCE_API_URL + POWNCE_API_VERSION + url;
        }
    }

};

pownce.dom.iframedoc().open();
pownce.dom.iframedoc().writeln("<script language=\"Javascript\">function publicnotelistcallback(input) { window.parent.pownce.api.json.publicnotelist = input;window.parent.pownce.dom.functions.publicnotelist();}</script>");
pownce.dom.iframedoc().writeln("<script language=\"Javascript\">function userpublicnotelistcallback(input){ window.parent.pownce.api.json.userpublicnotes = input;window.parent.pownce.dom.functions.userpublicnotes();}</script>");
pownce.dom.iframedoc().writeln("<script language=\"Javascript\">function userprofilecallback(input) { window.parent.pownce.api.json.userprofile = input;window.parent.pownce.dom.functions.userprofile();}</script>");
pownce.dom.iframedoc().writeln("<script language=\"Javascript\">function publicnotecallback(input){ window.parent.pownce.api.json.publicnote = input;window.parent.pownce.dom.functions.publicnote();}</script>");
pownce.dom.iframedoc().writeln("<script language=\"Javascript\">function recipientlistcallback(input){window.parent.pownce.api.json.recipientlist = input;window.parent.pownce.dom.functions.recipientlist();}</script>");
pownce.dom.iframedoc().writeln("<script language=\"Javascript\">function userfollowerscallback(input){window.parent.pownce.api.json.userfollowers = input;window.parent.pownce.dom.functions.userfollowers();}</script>");
pownce.dom.iframedoc().close();

function userpublicnotelistcallback(input){ 
	pownce.api.json.userpublicnotes = input;
	pownce.dom.functions.userpublicnotes();
}

function userprofilecallback(input) { 
	pownce.api.json.userprofile = input;
	pownce.dom.functions.userprofile();
}

function publicnotelistcallback(input) { 
	pownce.api.json.publicnotelist = input;
	pownce.dom.functions.publicnotelist();
}

function publicnotecallback(input){
    pownce.api.json.publicnote = input;
	pownce.dom.functions.publicnote();
}

function recipientlistcallback(input){
    pownce.api.json.recipientlist = input;
	pownce.dom.functions.recipientlist();
}

function userfollowerscallback(input){
    pownce.api.json.userfollowers = input;
	pownce.dom.functions.userfollowers();
}




